package com.gw.booking.redis;

import redis.clients.jedis.Jedis;

public class RedisManage {



	// 保存数据设置过期时间
	public static void setData(String key, String value) {
		Jedis redisClient = null;
		redisClient = RedisClientPool.jedisPool.getResource();
		try {

			redisClient.set(key, value);

		} finally {
			// 关闭连接
			redisClient.close();
		}
	}

	// 保存数据设置过期时间
	public static void setData(String key, String value, Integer time) {
		Jedis redisClient = null;
		redisClient = RedisClientPool.jedisPool.getResource();
		try {

			redisClient.set(key, value);
			redisClient.expire(key, time);

		} finally {
			// 关闭连接
			if (redisClient != null) {
				redisClient.close();
			}
		}
	}

	// 仅保存数据，当key不存在，并设置过期时间
	public static Long setDataNX(String key, String value, Integer time) {
		Jedis redisClient = null;
		Long exist = 0l;
		redisClient = RedisClientPool.jedisPool.getResource();
		try {

			exist = redisClient.setnx(key, value);
			redisClient.expire(key, time);

		} finally {
			// 关闭连接
			if (redisClient != null) {
				redisClient.close();
			}
		}
		return exist;
	}

	// 根据key取值
	public static String getData(String key) {
		String data = null;
		Jedis redisClient = null;
		try {
			redisClient = RedisClientPool.jedisPool.getResource();
			data = redisClient.get(key);
		}

		finally {
			// 关闭连接
			if (redisClient != null) {
				redisClient.close();
			}
		}

		return data;
	}

	// 获取key的过期时间
	public static Long getMin(String key) {
		Long min = null;
		Jedis redisClient = null;
		try {
			redisClient = RedisClientPool.jedisPool.getResource();
			min = redisClient.ttl(key);

		} finally {
			// 关闭连接
			if (redisClient != null) {
				redisClient.close();
			}
		}
		return min;
	}
	
	// 根据key取值
		public static void delData(String key) {
			Jedis redisClient = null;
			try {
				redisClient = RedisClientPool.jedisPool.getResource();
				redisClient.del(key);
			}

			finally {
				// 关闭连接
				if (redisClient != null) {
					redisClient.close();
				}
			}

		}


}
